package clinica;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComboBox;
import java.awt.event.WindowFocusListener;

public class CadCliente extends JFrame {
	private int id = 1;

	private JPanel contentPane;
	private JTextField txtNome;
	private JTextField txtSobrenome;
	private JTextField txtEmail;
	private JTextField txtTelefone;
	private JTextField txtEndereco;
	private JTextField txtCpf;
	private JButton btnSalvar;
	private JButton btnCancelar;
	private JComboBox cbbNomePet;
	private JButton btnNovo;
	private JLabel lblId;
	protected static Cliente acme;
	
	private CadAnimal telaCadastroAnimal = new CadAnimal();
	protected static JButton btnVisualizar;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadCliente frame = new CadCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */

	public CadCliente() {
		addWindowFocusListener(new WindowFocusListener() {
			public void windowGainedFocus(WindowEvent arg0) {
				if((btnSalvar.getText().equals("Salvar"))&&(acme.pets.size()>cbbNomePet.getItemCount()))
					
				cbbNomePet.addItem(acme.pets.get(acme.pets.size()-1));
			}
			public void windowLostFocus(WindowEvent arg0) {
			}
		});
		addWindowListener(new WindowAdapter() {

			public void windowActivated(WindowEvent e) {
				lblId.setText(String.valueOf(id));
			}
		});

		setTitle("Cadastro De Cliente\r\n");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 750);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblCadastroDeCliente = new JLabel("Cadastro de Cliente");
		lblCadastroDeCliente.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblCadastroDeCliente.setBounds(138, 31, 153, 21);
		contentPane.add(lblCadastroDeCliente);

		txtNome = new JTextField();
		txtNome.setColumns(10);
		txtNome.setBounds(20, 122, 279, 20);
		contentPane.add(txtNome);

		JLabel lblNomeDoCliente = new JLabel("Nome Do Cliente");
		lblNomeDoCliente.setBounds(20, 97, 251, 14);
		contentPane.add(lblNomeDoCliente);

		JLabel lblSobrenomeDoCliente = new JLabel("Sobrenome Do Cliente");
		lblSobrenomeDoCliente.setBounds(20, 172, 251, 14);
		contentPane.add(lblSobrenomeDoCliente);

		txtSobrenome = new JTextField();
		txtSobrenome.setColumns(10);
		txtSobrenome.setBounds(20, 201, 279, 20);
		contentPane.add(txtSobrenome);

		JLabel lblEmailDoCliente = new JLabel("Email Do Cliente");
		lblEmailDoCliente.setBounds(20, 242, 208, 14);
		contentPane.add(lblEmailDoCliente);

		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(20, 271, 279, 20);
		contentPane.add(txtEmail);

		JLabel lblTelefoneDoCliente = new JLabel("Telefone Do Cliente");
		lblTelefoneDoCliente.setBounds(20, 320, 279, 14);
		contentPane.add(lblTelefoneDoCliente);

		txtTelefone = new JTextField();
		txtTelefone.setColumns(10);
		txtTelefone.setBounds(20, 345, 279, 20);
		contentPane.add(txtTelefone);

		JLabel lblEndereoDoCliente = new JLabel("Endere\u00E7o Do Cliente");
		lblEndereoDoCliente.setBounds(20, 404, 307, 14);
		contentPane.add(lblEndereoDoCliente);

		txtEndereco = new JTextField();
		txtEndereco.setColumns(10);
		txtEndereco.setBounds(20, 433, 279, 20);
		contentPane.add(txtEndereco);

		JLabel lblCpfDoCliente = new JLabel("CPF Do Cliente");
		lblCpfDoCliente.setBounds(20, 486, 251, 14);
		contentPane.add(lblCpfDoCliente);

		txtCpf = new JTextField();
		txtCpf.setColumns(10);
		txtCpf.setBounds(20, 515, 279, 20);
		contentPane.add(txtCpf);

		btnSalvar = new JButton("Cadastrar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (btnSalvar.getText().equals("Cadastrar")) {
					btnSalvar.setText("Salvar");
					btnNovo.setEnabled(true);
					
					acme = new Cliente(id, txtNome.getText(), txtSobrenome
							.getText(), txtEmail.getText(), txtTelefone
							.getText(), txtEndereco.getText(), txtCpf.getText());

					
					
					txtNome.setEditable(false);
					txtSobrenome.setEditable(false);
					txtEmail.setEditable(false);
					txtTelefone.setEditable(false);
					txtEndereco.setEditable(false);
					txtCpf.setEditable(false);
				}
				else{
					DataBase.addClientes(acme);
					id++;
					txtNome.setText(null);
					txtSobrenome.setText(null);
					txtEmail.setText(null);
					txtTelefone.setText(null);
					txtEndereco.setText(null);
					txtCpf.setText(null);
					
					txtNome.setEditable(true);
					txtSobrenome.setEditable(true);
					txtEmail.setEditable(true);
					txtTelefone.setEditable(true);
					txtEndereco.setEditable(true);
					txtCpf.setEditable(true);
					
					btnSalvar.setText("Cadastrar");
					
					btnNovo.setEnabled(false);
					
					dispose();
					
				}

			}
		});
		btnSalvar.setBounds(33, 655, 91, 23);
		contentPane.add(btnSalvar);

		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				

				txtNome.setText(null);
				txtSobrenome.setText(null);
				txtEmail.setText(null);
				txtTelefone.setText(null);
				txtEndereco.setText(null);
				txtCpf.setText(null);
				
				txtNome.setEditable(true);
				txtSobrenome.setEditable(true);
				txtEmail.setEditable(true);
				txtTelefone.setEditable(true);
				txtEndereco.setEditable(true);
				txtCpf.setEditable(true);
				
				btnSalvar.setText("Cadastrar");
				
				btnNovo.setEnabled(false);
				btnVisualizar.setEnabled(false);
				
				dispose();
			}
		});
		btnCancelar.setBounds(155, 655, 91, 23);
		contentPane.add(btnCancelar);

		JLabel lblIdCliente = new JLabel("Id Cliente");
		lblIdCliente.setBounds(321, 21, 65, 14);
		contentPane.add(lblIdCliente);

		lblId = new JLabel("");
		lblId.setBounds(386, 11, 46, 24);
		contentPane.add(lblId);

		cbbNomePet = new JComboBox();
		cbbNomePet.setBounds(20, 582, 172, 22);
		contentPane.add(cbbNomePet);

		JLabel lblNomeDoAnimail = new JLabel("Nome do Animail");
		lblNomeDoAnimail.setBounds(20, 557, 106, 14);
		contentPane.add(lblNomeDoAnimail);

		btnNovo = new JButton("Novo");
		btnNovo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				telaCadastroAnimal.setVisible(true);
			}
		});
		btnNovo.setEnabled(false);
		btnNovo.setBounds(208, 582, 91, 23);
		contentPane.add(btnNovo);
		
		btnVisualizar = new JButton("Visualizar Pet");
		btnVisualizar.setEnabled(false);
		btnVisualizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(cbbNomePet.getSelectedItem() instanceof Animal) {
					
					Animal  pet =(Animal) cbbNomePet.getSelectedItem();
					JOptionPane.showMessageDialog(null,
							"ID: "+ pet.getId()+
							"\nNome: " + pet.getNome()+
							"\nEspecie: "+pet.getEspecie()+
							"\nPeso: "+pet.getPeso()+
							"\nIdade: "+pet.getIdade()+
							"\nRaca: "+pet.getRaca()+
							"\nSexo: "+pet.getSexo()
							);
					
				
					
				}
				
			}
		});
		btnVisualizar.setBounds(284, 655, 123, 23);
		contentPane.add(btnVisualizar);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtNome, txtSobrenome, txtEmail, txtTelefone, txtEndereco, txtCpf, btnSalvar, btnCancelar, cbbNomePet, btnNovo}));
	}
}
